package LECONGHAU.SWING_DEMO;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import java.awt.FlowLayout;
import net.miginfocom.swing.MigLayout;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Views extends JFrame {

	private JPanel contentPane;
	private JTextField txtTenTruyCap;
	private JPasswordField txtMatKhau;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Views frame = new Views();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Views() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Views.class.getResource("/LECONGHAU/SWING_DEMO/ICON/Go.png")));
		setResizable(false);
		setTitle("ROOM");
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "\u0110\u0103ng nh\u1EADp", TitledBorder.LEADING, TitledBorder.TOP, null, Color.RED));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("Vui lòng nhập đầy đủ thông tin");
		
		JLabel lblTnTruyCp = new JLabel("Tên truy cập");
		
		JLabel lblMtKhu = new JLabel("Mật khẩu");
		
		txtTenTruyCap = new JTextField();
		txtTenTruyCap.setBackground(new Color(0, 191, 255));
		txtTenTruyCap.setColumns(10);
		
		JButton btnThoat = new JButton("Thoát");
		btnThoat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//Đây nữa
			}
		});
		btnThoat.setIcon(new ImageIcon(Views.class.getResource("/LECONGHAU/SWING_DEMO/ICON/delete.gif")));
		
		JButton btnDangNhap = new JButton("Đăng nhập");
		btnDangNhap.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Code cai gi
				//1. Kiểm tra trạng thái điều khiển trên frame?
				if(txtTenTruyCap.getText().isEmpty()){
					//JOptionPane.showMessageDialog(getParent(), "Bạn cần nhập vào tên truy cập");
					JOptionPane.showMessageDialog(getParent(), "Bạn cần nhập vào tên truy cập", "Thông báo", JOptionPane.ERROR_MESSAGE);
					//JOptionPane.showMessageDialog(getParent(), "Bạn cần nhập vào tên truy cập", "Thông báo", JOptionPane.WARNING_MESSAGE, new ImageIcon(Views.class.getResource("/LECONGHAU/SWING_DEMO/ICON/Login Manager32x32.png")));
				} else if(txtMatKhau.getText().isEmpty()) {
					JOptionPane.showMessageDialog(getParent(), "Bạn cần nhập vào tên truy cập");
				} else {
					//2. Thực thi kiểm tra tài khoản
					//a. Khai báo vào khởi tạo Controller
					Controllers con = new Controllers();
					
					//b. Nhận các giá trị 
					Entity objEntity = new Entity();
					objEntity.setUserName(txtTenTruyCap.getText());
					objEntity.setPassWord(txtMatKhau.getText());
					
					//c. THực thi
					if(con.CheckUserInfo(objEntity)) {
						JOptionPane.showMessageDialog(getParent(), "Có lẽ đúng");
					} else {
						JOptionPane.showMessageDialog(getParent(), "Có lẽ sai");
					}
				}
			}
		});
		btnDangNhap.setIcon(new ImageIcon(Views.class.getResource("/LECONGHAU/SWING_DEMO/ICON/Login Manager32x32.png")));
		
		txtMatKhau = new JPasswordField();
		txtMatKhau.setBackground(new Color(0, 191, 255));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(33)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblTnTruyCp)
								.addComponent(lblMtKhu))
							.addGap(57)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(txtMatKhau, 245, 245, 245)
								.addComponent(txtTenTruyCap, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(btnDangNhap)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(btnThoat, GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))))
					.addGap(36))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(44)
					.addComponent(lblNewLabel)
					.addGap(32)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblTnTruyCp)
						.addComponent(txtTenTruyCap, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMtKhu)
						.addComponent(txtMatKhau, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnThoat, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnDangNhap, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(31, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
